package com.rz.paas.baselib.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.rz.paas.baselib.R


class CustomProgressView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {
    private var cp_percent_textsize = 18 //百分比字体大小

    private var cp_percent_textcolor = -0xff6533
    private var cp_background_color = -0x9c9c9d
    private var cp_progress_color = -0xff3a33
    private var cp_background_is_stroke = true
    private var cp_rect_round = 5
    private val mPaint: Paint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }
    private var mCenterX = 0
    private var mCenterY = 0

    private var progressCurrent = 0
    private var progressMax = 100

    init {
        val typedArray =
            context.obtainStyledAttributes(attributeSet, R.styleable.CustomProgressView)

        cp_percent_textsize = typedArray.getDimensionPixelSize(
            R.styleable.CustomProgressView_cp_percent_text_size, dip2px(
                cp_percent_textsize
            )
        );
        cp_percent_textcolor = typedArray.getColor(
            R.styleable.CustomProgressView_cp_percent_text_color,
            cp_percent_textcolor
        )
        cp_background_color = typedArray.getColor(
            R.styleable.CustomProgressView_cp_background_color,
            cp_background_color
        )
        cp_progress_color =
            typedArray.getColor(R.styleable.CustomProgressView_cp_progress_color, cp_progress_color)
        cp_background_is_stroke = typedArray.getBoolean(
            R.styleable.CustomProgressView_cp_background_is_stroke,
            cp_background_is_stroke
        )
        cp_rect_round =
            typedArray.getDimensionPixelSize(
                R.styleable.CustomProgressView_cp_rect_round, dip2px(
                    cp_rect_round
                )
            );


        typedArray.recycle()

    }

    private fun dip2px(dp: Int): Int {
        var applyDimension = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp * 1.0f,
            resources.displayMetrics
        )
        var toInt = applyDimension.toInt()
        return toInt

    }

    override fun onDraw(canvas: Canvas) {
        mCenterX = width / 2
        mCenterY = height / 2
        drawHorizontal(mPaint, canvas)
    }

    private fun drawHorizontal(mPaint: Paint, canvas: Canvas) {

        mPaint.setColor(cp_background_color)

        if (cp_background_is_stroke) {
            mPaint.style = Paint.Style.STROKE
            mPaint.strokeWidth = 1f
        } else {
            mPaint.style = Paint.Style.FILL
        }

        canvas.drawRoundRect(
            RectF(
                (mCenterX - width / 2) * 1.0f,
                (mCenterY - height / 2) * 1.0f,
                (mCenterX + width / 2) * 1.0f,
                (mCenterY + height / 2) * 1.0f
            ),
            cp_rect_round * 1.0f, cp_rect_round * 1.0f, mPaint
        )

        mPaint.color = cp_progress_color
        mPaint.style = Paint.Style.FILL
        canvas.drawRoundRect(
            RectF(
                mCenterX - width / 2 * 1.0f + 5,
                mCenterY - height / 2 * 1.0f + 5,
                progressCurrent * width / progressMax * 1.0f - 5,
                mCenterY + height / 2 * 1.0f - 5
            ),
            cp_rect_round * 1.0f, cp_rect_round * 1.0f, mPaint
        )

        mPaint.color = cp_percent_textcolor
        mPaint.textSize = cp_percent_textsize * 1.0f
        mPaint.style = Paint.Style.FILL
        var value: String = (progressCurrent * 1.0f * 100 / (progressMax * 1.0f)).toString() + "%"

        var rect = Rect()
        mPaint.getTextBounds(value, 0, value.length, rect)

        var textWidth = rect.width()
        var textHeight = rect.height()
        if (textWidth >= width) {
            textWidth = width
        }

        var fontMetrics = mPaint.getFontMetrics()
        var dy = (fontMetrics.top - fontMetrics.bottom) / 2 + fontMetrics.bottom
        var baseLine = width / 2 + dy
        canvas.drawText(value, mCenterX - textWidth / 2 * 1.0f, baseLine, mPaint)
    }

    fun getProgressCurrent(): Int {

        return progressCurrent
    }

    fun setProgressCurrent(progressCurrent: Int) {
        if (progressCurrent > progressMax) {
            this.progressCurrent = progressMax
        } else {
            this.progressCurrent = progressCurrent
        }
        postInvalidate()
    }

    fun setProgressMax(progress: Int) {
        this.progressMax = progress
    }
}